<template>
  <div>
    <el-form inline="true" :model="searchData">
      <el-form-item label="学号">
        <el-input v-model="searchData.username"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getScore">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table border="true" :data="studentData">
      <el-table-column label="学号" prop="username"></el-table-column>
      <el-table-column label="学生姓名" prop="nickname"></el-table-column>
      <el-table-column
        v-for="course in courseData"
        :label="course.name"
        :prop="course.name"
        :key="course.id"
      >
      </el-table-column>
      <el-table-column label="综测分" :formatter="getZc"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="selectShowLog(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
        @size-change="getScore"
        @current-change="getScore"
        :current-page.sync="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="分数上报"
      ref="scoreIns"
      :visible.sync="scoreShow"
      width="30%"
    >
      <el-form inline="true" :model="courseForm">
        <el-form-item label="大学物理">
          <el-input v-model="courseForm.大学物理"></el-input>
        </el-form-item>
        <el-form-item label="高等数学">
          <el-input v-model="courseForm.高等数学"></el-input>
        </el-form-item>
        <el-form-item label="大学英语">
          <el-input v-model="courseForm.大学英语"></el-input>
        </el-form-item>
        <el-form-item label="程序设计">
          <el-input v-model="courseForm.程序设计"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="scoreShow = false">取 消</el-button>
        <el-button type="primary" @click="submitScore">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.getCourse();
    this.getzfbfb();
    this.getScore();
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      StuNo: 0,
      scoreShow: false,
      pageSize: 10,
      studentData: [],
      courseData: [],
      searchData: { username: "" },
      courseForm: {},
      zfbfb: [],
    };
  },
  methods: {
    gets(row) {
      console.log(row);
    },
    selectShowLog(row) {
      this.scoreShow = true;
      //查询成绩
      this.request.get("/score/getScore/" + row.username).then((resp) => {
        this.courseForm = resp.data;
      });
      this.StuNo = row.username;
    },
    getCourse() {
      this.request.get("/course").then((res) => {
        this.courseData = res.data;
      });
    },
    //获取学生成绩
    getScore() {
      this.request
        .get("/score/select", {
          params: {
            username: this.searchData.username,
            pageSize: this.pageSize,
            pageNum: this.pageNum,
          },
        })
        .then((resp) => {
          this.studentData = resp.data.records;
          this.total = resp.data.total;
        });
    },
    submitScore() {
      //分数提交
      this.request
        .post("/score/updateFen/" + this.StuNo, this.courseForm)
        .then((resp) => {
          if (resp.code == 200) {
            //
            this.scoreShow = false;
            this.$message({
              message: "保存成功",
              type: "success",
            });
            this.getScore();
          }
        });
    },
    getzfbfb() {
      this.request.get("/score/getZc").then((reps) => {
        this.zfbfb = reps.data;
      });
    },
    getZc(row) {
      let fen = 0.0;
      this.zfbfb.forEach((element) => {
        if (element.name == "大学物理") {
          //计算大学物理分数
          fen += (parseFloat(row.大学物理) * parseFloat(element.weight)) / 100;
        } else if (element.name == "高等数学") {
          //计算高等数学分数
          fen += (parseFloat(row.高等数学) * parseFloat(element.weight)) / 100;
        } else if (element.name == "大学英语") {
          //计算大学英语分数
          fen += (parseFloat(row.大学英语) * parseFloat(element.weight)) / 100;
        } else {
          //计算程序设计分数
          fen += (parseFloat(row.程序设计) * parseFloat(element.weight)) / 100;
        }
      });
      return fen;
    },
  },
};
</script>

<style>
</style>